package com.bis.ymyy.controller;

import com.bis.ymyy.common.utils.QueryPage;
import com.bis.ymyy.common.utils.Result;
import com.bis.ymyy.entity.History;
import com.bis.ymyy.service.HistoryService;
import com.bis.ymyy.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/history")
@Api(tags = "/接种记录相关接口")
public class HistoryController {
    @Autowired(required = false)
    private HistoryService historyService;
    @Autowired(required = false)
    private UserService userService;

    @ApiOperation("/分页查找所有接种记录")
    @PostMapping("/findAllHistory")
    public Result<PageInfo> findAllHistory(@RequestBody(required = false) QueryPage page){
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<History> models = historyService.getAll();
        PageInfo<History> pageInfo = new PageInfo<>(models);
        Result<PageInfo> result = new Result<>();
        result.setSuccess(true);
        result.setCode(200);
        result.setMsg("获取成功");
        result.setData(pageInfo);
        return result;
    }

    @ApiOperation("/根据用户名查找接种记录")
    @GetMapping("/findHistoryByName")
    public Result<History> findHistoryByName(@RequestParam(value = "userName") String userName) {
        Result<History> result = new Result<>();
        if (userName.equals("")){
            result.setCode(400);
            result.setMsg("用户名参数为空");
            result.setSuccess(false);
        } else {
            History history = historyService.getOne(userName);
            if (history == null) {
                result.setSuccess(false);
                if (userService.findUserByUserName(userName) == null) {
                    result.setMsg("未查询到该用户");
                    result.setCode(400);
                } else {
                    result.setCode(400);
                    result.setMsg("该用户暂未接种疫苗");
                }
            } else {
                result.setData(history);
                result.setCode(200);
                result.setMsg("查找成功");
                result.setSuccess(true);
            }
        }
        return result;
    }

    @ApiOperation("/新增一条记录")
    @PostMapping("/addOneHistory")
    public Result<History> addOneHistory(@RequestBody History history){
        Result<History> result = new Result();
        if (history == null) {
            result.setCode(400);
            result.setMsg("参数为空");
            result.setSuccess(false);
        } else {
            if( historyService.getOne(history.getUserName()) != null ){
                result.setCode(400);
                result.setMsg("不能重复添加");
                result.setSuccess(false);
                result.setData(history);
            } else {
                historyService.addOne(history);
                result.setMsg("添加记录成功");
                result.setCode(200);
                result.setSuccess(true);
            }
        }
        return result;
    }

    @ApiOperation("/更新记录")
    @PostMapping("/updateHistory")
    public Result<History> updateHistory(@RequestBody History history){
        Result<History> result = new Result<>();
        if (history == null) {
            result.setCode(400);
            result.setMsg("参数为空");
            result.setSuccess(false);
        } else {
            if(historyService.updateOne(history) == 1){
                result.setMsg("更新记录成功");
                result.setCode(200);
                result.setSuccess(true);
            } else {
                result.setSuccess(false);
                result.setCode(400);
                result.setMsg("更新记录失败");
            }
            result.setData(history);
        }
        return result;
    }

    @ApiOperation("/删除记录")
    @GetMapping("/deleteHistory")
    public Result<History> deleteHistory(@RequestParam String userName){
        Result<History> result = new Result<>();
        if (userName.equals("")){
            result.setCode(400);
            result.setMsg("用户名参数为空");
            result.setSuccess(false);
        } else {
            if (historyService.getOne(userName) == null){
                result.setCode(400);
                result.setMsg("该记录已删除或没有该用户");
                result.setSuccess(false);
            } else {
                if(historyService.deleteOne(userName) == 1) {
                    result.setCode(200);
                    result.setMsg("删除记录成功");
                    result.setSuccess(true);
                } else {
                    result.setCode(400);
                    result.setMsg("该记录已删除或没有该用户");
                    result.setSuccess(false);
                }
            }
        }
        return result;
    }
}
